#include "pos.h"

int pos(int lcdp[][800], list *s, int *cho, int *x, int *y, int *fy, int *color, int *flag)
{
	int x0, y0;
	int i, j;
	int lcdt = open("/dev/input/event0", O_RDWR);
	if (lcdt < 0)
	{
		perror("input error\n");
		return -1;
	}
	struct input_event touch;
	static time_t start, end;
	font *ef = fontLoad("/usr/share/fonts/DroidSansFallback.ttf");
	fontSetSize(ef, 15);
	while (1)
	{
		int read_ret = read(lcdt, &touch, sizeof(struct input_event));
		if (read_ret < 0)
		{
			perror("read touch failed\n");
			return -1;
		}
		if (touch.type == EV_ABS)
		{
			if (touch.code == ABS_X)
				*x = touch.value * 800 / 1024;
			else if (touch.code == ABS_Y)
			{
				*y = touch.value * 480 / 600;
			}
		}
		printf("cho=%d, x=%d y=%d x0=%d y0=%d\n", *cho, *x, *y, x0, y0);
		if (touch.type == EV_KEY)
		{
			if (touch.code == BTN_TOUCH && touch.value == 1)
			{
				if ((*x > 0 && *x < 750) && (*y > 0 && *y < 480))
				{
					x0 = *x;
					y0 = *y;
				}
				else if ((*x > 750 && *x <= 800) && (*y > 290 && *y < 480))
				{
					time(&start);
					printf("start\n");
				}
			}
			else if (touch.code == BTN_TOUCH && touch.value == 0)
			{
				if ((*x > 0 && *x < 750) && (*y > 0 && *y < 480))
				{
					x0 = -1;
					y0 = -1;
					printf("记录中 s->len:%d s->posi:%d\n", s->len, s->posi);
					push(s, lcdp);
				}
				else if ((*x > 750 && *x <= 800) && (*y > 290 && *y < 480))
				{
					time(&end);
					printf("end\n");
					double times = difftime(end, start);
					if (times < 1)
					{
						if (*x > 755 && *x < 800 && *y >= 300 && *y < 360)
							*flag = 0;
						if (*x > 755 && *x < 800 && *y >= 360 && *y < 420)
						{
							printf("快进中 s->posi:%d\n", s->posi);
							pull(s, lcdp);
							char tip[] = "快进";
							bitmap *er = createBitmapWithInit(100, 15, 4, getColor(255, 255, 255, 255));
							fontPrint(ef, er, 0, 0, tip, getColor(0, 0, 0, 255), 400);
							show_font_to_lcd(lcdp, 760, 5, er);
							sleep(1);
							er = createBitmapWithInit(100, 15, 4, getColor(255, 255, 255, 255));
							fontPrint(ef, er, 0, 0, tip, getColor(255, 255, 255, 255), 400);
							show_font_to_lcd(lcdp, 760, 5, er);
						}
						if (*x > 755 && *x < 800 && *y >= 420 && *y < 480)
						{
							perfor(lcdp);
							push(s, lcdp);
							printf("clear\n");
							char tip[] = "清空";
							bitmap *er = createBitmapWithInit(100, 15, 4, getColor(255, 255, 255, 255));
							fontPrint(ef, er, 0, 0, tip, getColor(0, 0, 0, 255), 400);
							show_font_to_lcd(lcdp, 760, 5, er);
							sleep(1);
							er = createBitmapWithInit(100, 15, 4, getColor(255, 255, 255, 255));
							fontPrint(ef, er, 0, 0, tip, getColor(255, 255, 255, 255), 400);
							show_font_to_lcd(lcdp, 760, 5, er);
						}
					}
					else if (times >= 1)
					{
						if (*x > 755 && *x < 800 && *y >= 300 && *y < 360)
						{
							printf("撤回中 s->posi:%d\n", s->posi);
							pop(s, lcdp);
							char tip[] = "撤回";
							bitmap *er = createBitmapWithInit(100, 15, 4, getColor(255, 255, 255, 255));
							fontPrint(ef, er, 0, 0, tip, getColor(0, 0, 0, 255), 400);
							show_font_to_lcd(lcdp, 760, 5, er);
							sleep(1);
							er = createBitmapWithInit(100, 15, 4, getColor(255, 255, 255, 255));
							fontPrint(ef, er, 0, 0, tip, getColor(255, 255, 255, 255), 400);
							show_font_to_lcd(lcdp, 760, 5, er);
						}
						else if (*x > 755 && *x < 800 && *y >= 360 && *y < 420)
						{
							printf("保存中\n");
							char tip[] = "保存";
							bitmap *er = createBitmapWithInit(100, 15, 4, getColor(255, 255, 255, 255));
							fontPrint(ef, er, 0, 0, tip, getColor(0, 0, 0, 255), 400);
							show_font_to_lcd(lcdp, 760, 5, er);
							sleep(1);
							er = createBitmapWithInit(100, 15, 4, getColor(255, 255, 255, 255));
							fontPrint(ef, er, 0, 0, tip, getColor(255, 255, 255, 255), 400);
							show_font_to_lcd(lcdp, 760, 5, er);
							char filename[20];
							sprintf(filename, "./lib/%s %d.txt", username, picnum++);
							FILE *reco = fopen(filename, "w");
							for (i = 0; i < 480; i++)
							{
								for (j = 0; j < 750; j++)
									fprintf(reco, "%d ", lcdp[i][j]);
								fprintf(reco, "\n");
							}
							printf("保存完毕\n");
							fclose(reco);
						}
						else if (*x > 755 && *x < 800 && *y >= 420 && *y < 480)
						{
							printf("退出中\n");
							close(lcdt);
							*cho = 3;
							return 3;
						}
					}
				}
			}
		}
		int dx, dy, steps;
		float x_inc, y_inc, x_temp, y_temp;
		if (x0 != -1 && y0 != -1) // 确保起点有效
		{
			// 计算点之间的距离
			dx = *x - x0 > 0 ? *x - x0 : x0 - *x;
			dy = *y - y0 > 0 ? *y - y0 : y0 - *y;
			// 选择步数为较大的轴差值
			steps = dx > dy ? dx : dy;
			x_inc = (float)(*x - x0) / (float)steps;
			y_inc = (float)(*y - y0) / (float)steps;
			x_temp = x0;
			y_temp = y0;
		}
		if (touch.type == EV_SYN)
		{
			if (*flag == 1 && *x > 0 && *x < 750 && *y > 0 && *y < 480)
			{
				char tip[] = "画笔";
				bitmap *er = createBitmapWithInit(100, 15, 4, getColor(255, 255, 255, 255));
				fontPrint(ef, er, 0, 0, tip, getColor(0, 0, 0, 255), 400);
				show_font_to_lcd(lcdp, 760, 5, er);
				if (x0 != -1 && y0 != -1) // 确保起点有效
				{
					// 使用插值法绘制点，确保线条连续
					for (int i = 0; i <= steps; i++)
					{
						// 绘制当前点
						drawCircle(*cho, lcdp, (int)x_temp, (int)y_temp, *color);
						x_temp += x_inc;
						y_temp += y_inc;
					}
					x0 = *x;
					y0 = *y;
				}
			}
			else if (*flag == 0 && *x > 0 && *x < 750 && *y > 0 && *y < 480)
			{
				char tip[] = "橡皮";
				bitmap *er = createBitmapWithInit(100, 15, 4, getColor(255, 255, 255, 255));
				fontPrint(ef, er, 0, 0, tip, getColor(0, 0, 0, 255), 400);
				show_font_to_lcd(lcdp, 760, 5, er);
				// 使用插值法绘制点，确保线条连续
				for (int i = 0; i <= steps; i++)
				{
					// 绘制当前点
					drawCircle(*cho, lcdp, (int)x_temp, (int)y_temp, w);
					x_temp += x_inc;
					y_temp += y_inc;
				}
				x0 = *x;
				y0 = *y;
			}
			if (*x > 750 && *x < 800)
			{
				if (*y >= 140 && *y <= 290)
				{
					*flag = 1;
					if (*y > 140 && *y < 165)
					{
						*color = b;
						int rx = 775, ry = 25; // 圆心1 r=4|8
						for (i = 0; i < 480; i++)
							for (j = 750; j < 800; j++)
							{
								if ((j - rx) * (j - rx) + (i - ry) * (i - ry) == 16)
									lcdp[i][j] = b;
								else if ((j - rx) * (j - rx) + (i - ry) * (i - ry) < 16)
									lcdp[i][j] = b;
							}
						ry = 125; // 圆心3
						for (i = 0; i < 480; i++)
							for (j = 750; j < 800; j++)
							{
								if ((j - rx) * (j - rx) + (i - ry) * (i - ry) == 64)
									lcdp[i][j] = b;
								else if ((j - rx) * (j - rx) + (i - ry) * (i - ry) < 64)
									lcdp[i][j] = b;
							}
					}
					else if (*y >= 165 && *y < 190)
					{
						*color = r;
						int rx = 775, ry = 25; // 圆心1 r=4|8
						for (i = 0; i < 480; i++)
							for (j = 750; j < 800; j++)
							{
								if ((j - rx) * (j - rx) + (i - ry) * (i - ry) == 16)
									lcdp[i][j] = b;
								else if ((j - rx) * (j - rx) + (i - ry) * (i - ry) < 16)
									lcdp[i][j] = r;
							}
						ry = 125; // 圆心3
						for (i = 0; i < 480; i++)
							for (j = 750; j < 800; j++)
							{
								if ((j - rx) * (j - rx) + (i - ry) * (i - ry) == 64)
									lcdp[i][j] = b;
								else if ((j - rx) * (j - rx) + (i - ry) * (i - ry) < 64)
									lcdp[i][j] = r;
							}
					}
					else if (*y >= 190 && *y < 215)
					{
						*color = p;
						int rx = 775, ry = 25; // 圆心1 r=4|8
						for (i = 0; i < 480; i++)
							for (j = 750; j < 800; j++)
							{
								if ((j - rx) * (j - rx) + (i - ry) * (i - ry) == 16)
									lcdp[i][j] = b;
								else if ((j - rx) * (j - rx) + (i - ry) * (i - ry) < 16)
									lcdp[i][j] = p;
							}
						ry = 125; // 圆心3
						for (i = 0; i < 480; i++)
							for (j = 750; j < 800; j++)
							{
								if ((j - rx) * (j - rx) + (i - ry) * (i - ry) == 64)
									lcdp[i][j] = b;
								else if ((j - rx) * (j - rx) + (i - ry) * (i - ry) < 64)
									lcdp[i][j] = p;
							}
					}
					else if (*y >= 215 && *y < 240)
					{
						*color = ye;
						int rx = 775, ry = 25; // 圆心1 r=4|8
						for (i = 0; i < 480; i++)
							for (j = 750; j < 800; j++)
							{
								if ((j - rx) * (j - rx) + (i - ry) * (i - ry) == 16)
									lcdp[i][j] = b;
								else if ((j - rx) * (j - rx) + (i - ry) * (i - ry) < 16)
									lcdp[i][j] = ye;
							}
						ry = 125; // 圆心3
						for (i = 0; i < 480; i++)
							for (j = 750; j < 800; j++)
							{
								if ((j - rx) * (j - rx) + (i - ry) * (i - ry) == 64)
									lcdp[i][j] = b;
								else if ((j - rx) * (j - rx) + (i - ry) * (i - ry) < 64)
									lcdp[i][j] = ye;
							}
					}
					else if (*y >= 240 && *y < 265)
					{
						*color = bl;
						int rx = 775, ry = 25; // 圆心1 r=4|8
						for (i = 0; i < 480; i++)
							for (j = 750; j < 800; j++)
							{
								if ((j - rx) * (j - rx) + (i - ry) * (i - ry) == 16)
									lcdp[i][j] = b;
								else if ((j - rx) * (j - rx) + (i - ry) * (i - ry) < 16)
									lcdp[i][j] = bl;
							}
						ry = 125; // 圆心3
						for (i = 0; i < 480; i++)
							for (j = 750; j < 800; j++)
							{
								if ((j - rx) * (j - rx) + (i - ry) * (i - ry) == 64)
									lcdp[i][j] = b;
								else if ((j - rx) * (j - rx) + (i - ry) * (i - ry) < 64)
									lcdp[i][j] = bl;
							}
					}
					else if (*y >= 265 && *y < 290)
					{
						*color = g;
						int rx = 775, ry = 25; // 圆心1 r=4|8
						for (i = 0; i < 480; i++)
							for (j = 750; j < 800; j++)
							{
								if ((j - rx) * (j - rx) + (i - ry) * (i - ry) == 16)
									lcdp[i][j] = b;
								else if ((j - rx) * (j - rx) + (i - ry) * (i - ry) < 16)
									lcdp[i][j] = g;
							}
						ry = 125; // 圆心3
						for (i = 0; i < 480; i++)
							for (j = 750; j < 800; j++)
							{
								if ((j - rx) * (j - rx) + (i - ry) * (i - ry) == 64)
									lcdp[i][j] = b;
								else if ((j - rx) * (j - rx) + (i - ry) * (i - ry) < 64)
									lcdp[i][j] = g;
							}
					}
					else if (*y >= 300 && *y < 360)
					{
						int rx = 775, ry = 25;
						for (int i = 0; i < 480; i++)
							for (int j = 750; j < 800; j++)
							{
								if ((j - rx) * (j - rx) + (i - ry) * (i - ry) == 16)
									lcdp[i][j] = b;
								else if ((j - rx) * (j - rx) + (i - ry) * (i - ry) < 16)
									lcdp[i][j] = wh;
							}
						ry = 125; // 圆心3
						for (int i = 0; i < 480; i++)
							for (int j = 750; j < 800; j++)
							{
								if ((j - rx) * (j - rx) + (i - ry) * (i - ry) == 64)
									lcdp[i][j] = b;
								else if ((j - rx) * (j - rx) + (i - ry) * (i - ry) < 64)
									lcdp[i][j] = wh;
							}
					}
				}
				else if (*y >= 30 && *y <= 110)
				{
					for (j = 760; j < 790; j++)
					{
						lcdp[*fy][j] = w;
						if (j == 775)
							lcdp[*fy][j] = b;
					}
					*fy = *y;
					*cho = *fy;
					for (j = 760; j < 790; j++)
						lcdp[*fy][j] = r;
				}
			}
		}
	}
}